package edu.nf.meituan.dao.shop.impl;

import edu.nf.meituan.dao.shop.ShopAndItemsAndItemsTypeDAO;
import edu.nf.meituan.model.ltem.Item;
import edu.nf.meituan.model.ltem.ItemType;
import edu.nf.meituan.model.shop.Shop;
import edu.nf.meituan.model.shop.ShopAndItemsAndItemsType;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

public class ShopAndItemsAndItemsTypeDAOImpl implements ShopAndItemsAndItemsTypeDAO {
    @Override
    public ShopAndItemsAndItemsType listShopAndItemsAndItemsType(Integer shopId) {
        //获取连接对象
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        ShopAndItemsAndItemsType shopAndItemsAndItemsType = new ShopAndItemsAndItemsType();
        //sql
        String shopSql = "SELECT shop_id,shop_img,shop_types_id, u_id,shop_name,shop_phone,shop_address, shop_created_at FROM shop WHERE shop_id=?";
        String itemSql = "SELECT items_id,shop_id, items_type_id,items_picture,items_name,items_price, item_details FROM items WHERE shop_id = ?";
        String itemTypeSql = "SELECT items_type_id, shop_id, items_type_name, items_type_at  FROM items_type  WHERE shop_id = ?";

        BeanHandler<Shop> shopHandler = new BeanHandler<>(Shop.class);
        BeanListHandler<Item> itemBeanListHandler = new BeanListHandler<Item>(Item.class);
        BeanListHandler<ItemType> itemTypeBeanListHandler = new BeanListHandler<>(ItemType.class);

        Shop shop = sqlExecutor.executeQuery(shopSql, shopHandler, Shop.class,shopId);
        List<Item> items = sqlExecutor.executeQuery(itemSql, itemBeanListHandler, Item.class, shopId);
        List<ItemType> itemTypes = sqlExecutor.executeQuery(itemTypeSql, itemTypeBeanListHandler, shopId);

        shopAndItemsAndItemsType.setShop(shop);
        shopAndItemsAndItemsType.setItem(items);
        shopAndItemsAndItemsType.setItemType(itemTypes);
        return shopAndItemsAndItemsType;
    }
}
